#include <deviationfromexpectfilter.h>

DeviationFromExpectedFilter::DeviationFromExpectedFilter(void)
{
	mode = BetweenIn;
	devValue = 0.0;
}

DeviationFromExpectedFilter::~DeviationFromExpectedFilter(void)
{
}

bool DeviationFromExpectedFilter::accepts(AnatomicalLink* link)
{
	/*try
	{
		double dist = link->distance();
		double distinv = 1.0/dist;
		double corr = link->correlation();

		double devmean = network->mean();
		double devstd = network->std();

		double lval = devmean - z_value(confValue)*(devstd/sqrt((double)network->getLinks().size()));
		double rval = devmean + z_value(confValue)*(devstd/sqrt((double)network->getLinks().size()));

		switch(mode)
		{
		case BetweenIn:
			{
				if(abs(distinv-corr) >= lval && abs(distinv-corr) <= rval)
					return true;	
				else
					return false;
			}
		case BetweenOut:
			{
				if(abs(distinv-corr) <= lval || abs(distinv-corr) >= rval)
					return true;
				else
					return false;
			}
		default:
			return true;
		}
	}
	catch(std::exception e)
	{
		return true;
	}*/
	QVector<AnatomicalLink*> sortedList = network->getLinksSortedByDeviation();
	int nrsToTake = (int)(sortedList.size()*devValue);
	
	switch(mode)
	{
	case BetweenIn:
		{
			if(sortedList.indexOf(link)<=nrsToTake) //is at top of list
				return true;
			return false;
		}
	case BetweenOut:
		{
			if(sortedList.indexOf(link,sortedList.size()-nrsToTake)>=0) //is at bottom of list
				return true;
			return false;
		}
	default:
		return true;
	}
}
void DeviationFromExpectedFilter::setDeviationValue(double val)
{
	devValue = val;
	emit filterChanged(this);
}

void DeviationFromExpectedFilter::setFilterMode(FilterMode m)
{ 
	mode = m;
	emit filterChanged(this);
}

void DeviationFromExpectedFilter::setNetwork(FunctionalNetwork *net)
{ 
	network = net; 
	emit filterChanged(this);
}
